local L = AceLibrary("AceLocale-2.2"):new("Nier")

NierZone = Nier:NewModule("NierZone")

NierZone.options = {
	["Zones"] = {
		type = "group",
		name = L["Remove zones"],
		desc = L["Remove exception zones"],
		args = {},
	},
}

NierZone.defaultDB = {
	exceptedZones = {},
}

NierZone.toolTip = nil

local currentZone

function NierZone:OnEnable()
	if not NierEquip then
		self.core:Debug(L["There is no NierEquip"])
		return
	end

	self:RegisterEvent("ZONE_CHANGED", "CheckZone")
	self:RegisterEvent("ZONE_CHANGED_INDOORS", "CheckZone")
	self:RegisterEvent("ZONE_CHANGED_NEW_AREA", "CheckZone")

	self:CheckZone()
end

function NierZone:CheckZone()
	currentZone = GetRealZoneText()
	self.toolTip = {}
	if self.db.profile.exceptedZones[currentZone] then
		NierEquip.unequipSwitch["NierZone"] = true
		table.insert(self.toolTip, {
			["text"] = L["Equip gears in this area"],
			["func"] = self.UnregisterZone,
			["arg1"] = self,
		})
	else
		NierEquip.unequipSwitch["NierZone"] = false
		table.insert(self.toolTip, {
			["text"] = L["Unequip gears in this area"],
			["func"] = self.RegisterZone,
			["arg1"] = self,
		})
	end
	self.core:UpdateDisplay()
	self:ResetZoneMenu()
	NierEquip:CheckEquip()
end

function NierZone:RegisterZone(zoneName)
	if not zoneName then zoneName = currentZone end
	self.db.profile.exceptedZones[zoneName] = true
	self:CheckZone()
end

function NierZone:UnregisterZone(zoneName)
	if not zoneName then zoneName = currentZone end
	self.db.profile.exceptedZones[zoneName] = nil
	self:CheckZone()
end

function NierZone:ResetZoneMenu()
	local menuArgs = {}

	for k, v in pairs(self.db.profile.exceptedZones) do
		local menuItem = {
			type = "execute",
			name = k,
			func = function(zoneName)
				self:UnregisterZone(zoneName)
				self.core:OpenMenu()
			end,
			passValue = k,
			desc = L["Remove this zone"],
		}

		menuArgs[k] = menuItem
	end

	self.options["Zones"].args = menuArgs
end
